package org.example.controller;



import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.exception.model.ResponseResult;
import org.example.service.TestOneService;
import org.example.vo.ScanVo;
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @author yangzhenyu
 * */
@OpenAPIDefinition(
        info = @io.swagger.v3.oas.annotations.info.Info(
                title = "示例 API",
                version = "1.0",
                description = "这是一个示例 API 文档"
        ),
        tags = {
                @Tag(name = "Example", description = "示例 API")
        }
)
@Validated
@RestController
@Slf4j
@RequestMapping(value="api/test")
public class TestController {


    @Resource
    private TestOneService service;
    @Resource
    private CircuitBreakerFactory<?, ?> circuitBreakerFactory;

    @Operation(
            summary = "测试接口",
            description = "通过微服务调触发此测试接口",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
                    description = "调用测试接口的对象",
                    required = true,
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = ScanVo.class)
                    )
            ),
            responses = {
                    @ApiResponse(responseCode = "201", description = "测试成功"),
                    @ApiResponse(responseCode = "400", description = "请求错误")
            }
    )
    @CrossOrigin(origins = "*")  //处理跨域请求的注解
    @RequestMapping(value = "/scan", method = RequestMethod.POST)
    public ResponseResult scan(@Validated @RequestBody ScanVo scanVo) {
        String format = String.format("路径 %s!", scanVo.getPath());
        return ResponseResult.ok(format);
    }



    @Operation(
            summary = "测试微服务调用接口",
            description = "测试微服务调用接口",
            responses = {
                    @ApiResponse(
                            responseCode = "200",
                            description = "成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(implementation = ResponseResult.class)
                            )
                    ),
                    @ApiResponse(responseCode = "400", description = "请求错误")
            }
    )
    @CrossOrigin(origins = "*")  //处理跨域请求的注解
    @RequestMapping(value = "/weifuw/{path}", method = RequestMethod.GET)
    public ResponseResult weifuw(@PathVariable(value = "path") String path){
        log.info("=========调用服务B============参数:{}",path);
        return service.scan(path);
    }




    @GetMapping("/users")
    public String getUsers() {
        CircuitBreaker circuitBreaker = circuitBreakerFactory.create("userService");
        return circuitBreaker.run(() -> {
            // 模拟服务调用
            return "success";
        }, throwable -> "fallback");
    }
}
